package com.cyandpos.pad.cateringformat.choosemenu.model;

import android.text.TextUtils;
import android.util.Log;

import com.cyandpos.pad.common.commondata.BaseResponse;
import com.cyandpos.pad.common.commondata.ErpCommonData;
import com.cyandpos.pad.common.netmanager.PosHttpManager;
import com.cyandpos.pad.http.OkHttpUtils;
import com.cyandpos.pad.posdata.CyFood;
import com.cyandpos.pad.posdata.CyFoodData;
import com.cyandpos.pad.posdata.DjdData;
import com.cyandpos.pad.posdata.Goods;
import com.cyandpos.pad.posdata.Member;
import com.cyandpos.pad.posdata.OrderNoData;
import com.cyandpos.pad.posdata.PreparePayData;
import com.cyandpos.pad.posdata.PromationoodInfo;
import com.cyandpos.pad.posdata.QueryGoodParam;
import com.cyandpos.pad.posdata.QueryMemberReq;
import com.cyandpos.pad.posdata.QueryOrderReq;
import com.cyandpos.pad.posdata.RefundOrder;
import com.cyandpos.pad.posdata.SubTicketItem;
import com.cyandpos.pad.posdata.TicketInfo;
import com.google.gson.JsonObject;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

public class QueryFoodModelImpl implements QueryFoodModel {


    @Override
    public void loadQueryFood(String shopid, final OnLoadQueryFoodListener listener) {
        PosHttpManager.QueryFood(shopid, new OkHttpUtils.ResultCallback() {
            @Override
            public void onSuccess(JsonObject response) {
                Log.e("TAG","response==="+response);
                BaseResponse<CyFoodData> baseResponse = BaseResponse.fromJson(response.toString(),CyFoodData.class);
                if(baseResponse.getCode().equals("0")){
                    try {
                        listener.onLoadQueryFoodSuccess(baseResponse.getData().getData());
                    }catch (Exception e){
                        listener.onLoadQueryFoodFailure(baseResponse.getCode() + baseResponse.getMsg() + e.getMessage().toString());
                    }
                }else{
                    if (TextUtils.isEmpty(baseResponse.getTips())){
                        listener.onLoadQueryFoodFailure(baseResponse.getMsg());
                    }else {
                        listener.onLoadQueryFoodFailure(baseResponse.getTips());

                    }
                }
            }

            @Override
            public void onFailure(String msg) {
                listener.onLoadQueryFoodFailure(msg);
            }
        });
    }

    @Override
    public void createOrderNo(String orderNo,String tableNo ,String tablePerson,final onCreateOrderNoListener listener) {
        PosHttpManager.CreateSaleId(orderNo,tableNo,tablePerson, new OkHttpUtils.ResultCallback() {
            @Override
            public void onSuccess(JsonObject response) {
                BaseResponse<OrderNoData> baseResponse = BaseResponse.fromJson(response.toString(),OrderNoData.class);
                if(baseResponse.getCode().equals("0")){
                    listener.onCreateOrderNoSuccess(baseResponse.getData().getOrderNo());
                }else {
                    if (TextUtils.isEmpty(baseResponse.getTips())){
                        listener.onCreateOrderNoFailure(baseResponse.getMsg());
                    }else {
                        listener.onCreateOrderNoFailure(baseResponse.getTips());
                    }
                }
            }

            @Override
            public void onFailure(String msg) {
                listener.onCreateOrderNoFailure(msg);
            }
        });
    }

    @Override
    public void queryGoods(QueryGoodParam content, TicketInfo ticketInfo, int subInx, final OnQueryGoodsInfoListener listener) {
        PosHttpManager.QueryGoodsInfoExecute(content, ticketInfo,subInx, new OkHttpUtils.ResultCallback() {
            @Override
            public void onSuccess(JsonObject response) {
                Log.i("TAG----商品查询",response.toString());
                BaseResponse<Goods> baseResponse = BaseResponse.fromJson(response.toString(),Goods.class);
                if(baseResponse.getCode().equals("0")){
                    listener.onQueryGoodsInfoSuccess(baseResponse.getData());
                }else {
                    if (TextUtils.isEmpty(baseResponse.getTips())){
                        listener.onQueryGoodsInfoFailure(baseResponse.getMsg());
                    }else {
                        listener.onQueryGoodsInfoFailure(baseResponse.getTips());
                    }
                }
            }
            @Override
            public void onFailure(String msg) {
                listener.onQueryGoodsInfoFailure(msg);
            }
        });

    }

    @Override
    public void frontDiscount(String disType, String money, Goods goods, String orderNo, final boolean isSingle, final OnFrontDiscountListener listener) {
        PosHttpManager.FrontDiscount(disType, money, goods, orderNo, isSingle, new OkHttpUtils.ResultCallback() {
            @Override
            public void onSuccess(JsonObject response) {
                BaseResponse<TicketInfo> baseResponse = BaseResponse.fromJson(response.toString(),TicketInfo.class);
                if(baseResponse.getCode().equals("0")){
                    if(baseResponse.getData().getSubTickets()!=null&&baseResponse.getData().getSubTickets().get(0).getGoodsList().size()>0){
                        listener.onFrontDiscountSuccess(baseResponse.getData().getSubTickets().get(0).getGoodsList(),isSingle);
                    }else {
                        if (TextUtils.isEmpty(baseResponse.getTips())){
                            listener.onFrontDiscountFailure(baseResponse.getMsg());
                        }else {
                            listener.onFrontDiscountFailure(baseResponse.getMsg());
                        }
                    }
                }else {
                    if (TextUtils.isEmpty(baseResponse.getTips())){
                        listener.onFrontDiscountFailure(baseResponse.getMsg());
                    }else {
                        listener.onFrontDiscountFailure(baseResponse.getMsg());
                    }
                }
            }
            @Override
            public void onFailure(String msg) {
                listener.onFrontDiscountFailure(msg);
            }
        });
    }

    @Override
    public void queryMember(QueryMemberReq memberReq, final OnQueryMemberListener listener) {
        PosHttpManager.QueryMemberExecute(memberReq, new OkHttpUtils.ResultCallback() {
            @Override
            public void onSuccess(JsonObject response) {
                BaseResponse<Member> baseResponse = BaseResponse.fromJson(response.toString(),Member.class);
                if(baseResponse.getCode().equals("0")){
                    listener.OnQueryMemberSuccess(baseResponse.getData());
                }else {
                    if (TextUtils.isEmpty(baseResponse.getTips())){
                        listener.OnQueryMemberFailure(baseResponse.getMsg());
                    }else {
                        listener.OnQueryMemberFailure(baseResponse.getTips());
                    }
                }

            }

            @Override
            public void onFailure(String msg) {
                listener.OnQueryMemberFailure(msg);

            }
        });
    }

    @Override
    public void cancelOrder(String orderNo, final onCancelOrderListener listener) {
        PosHttpManager.cancelOrder(orderNo, new OkHttpUtils.ResultCallback() {
            @Override
            public void onSuccess(JsonObject response) {
                BaseResponse<OrderNoData> baseResponse = BaseResponse.fromJson(response.toString(),OrderNoData.class);
                if(baseResponse.getCode().equals("0")){
                    listener.onCancelOrderSuccess("");
                }else {
                    if (TextUtils.isEmpty(baseResponse.getTips())){
                        listener.onCancelOrderFailure(baseResponse.getMsg());
                    }else {
                        listener.onCancelOrderFailure(baseResponse.getTips());
                    }
                }
            }

            @Override
            public void onFailure(String msg) {
                listener.onCancelOrderFailure(msg);

            }
        });
    }

    @Override
    public void onPreparePay(final TicketInfo ticketInfo, final boolean isPromotion, final onPreparePayListener listener) {
        List<PromationoodInfo> exchangeGoodBillInfoList = new ArrayList<>();
        List<PromationoodInfo> giftGoodsBillList= new ArrayList<>();
        int promotionGoodsSelectSign = 0;
        if(ErpCommonData.isCatering){
            promotionGoodsSelectSign = 1;
        }else{
            promotionGoodsSelectSign = 0;
        }
        PosHttpManager.PreparePay(ticketInfo,isPromotion,exchangeGoodBillInfoList,giftGoodsBillList, promotionGoodsSelectSign,new OkHttpUtils.ResultCallback() {
            @Override
            public void onSuccess(JsonObject response) {
                Log.i("TAG----准备付款",response.toString());
                BaseResponse<PreparePayData> baseResponse = BaseResponse.fromJson(response.toString(),PreparePayData.class);
                if(baseResponse.getCode().equals("0")){
                    ticketInfo.setDecMoney(Math.abs(new BigDecimal(baseResponse.getData().getDecMoney()+"").doubleValue())+"");
                    for(SubTicketItem sub :ticketInfo.getSubTickets()){
                        sub.getGoodsList().clear();
                        for(Goods g: baseResponse.getData().getGoodsList()){
                            if(g.getTicktInx().equals(sub.getSubInx()) ){
                                Goods backGood = g.goodClone();
                                sub.getGoodsList().add(backGood);
                            }
                        }
                    }


//                    ticketInfo.getSubTickets().get(0).setGoodsList( baseResponse.getData().getGoodsList());
//                     if (ErpCommonData.isFSTH){
//                            if (baseResponse.getData().getTotalAmount().contains("-")){
//                                ticketInfo.setTotalAmount(baseResponse.getData().getTotalAmount().substring(1));
//                             }
//                            if (baseResponse.getData().getTotalCount().contains("-")){
//                                ticketInfo.setTotalCount(baseResponse.getData().getTotalCount().substring(1));
//                             }
//                     }else {
                    ticketInfo.setTotalAmount(Math.abs(new BigDecimal(baseResponse.getData().getTotalAmount()).doubleValue())+"");
                    ticketInfo.setTotalCount(Math.abs(new BigDecimal(baseResponse.getData().getTotalCount()).intValue())+"");
//                     }
                    ticketInfo.setDecPreferentialMoney(Math.abs(new BigDecimal(baseResponse.getData().getDecPreferentialMoney()).doubleValue())+"");
                    ticketInfo.setTotalDiscount(Math.abs(new BigDecimal(baseResponse.getData().getTotalDiscount()).doubleValue())+"");
                    ticketInfo.setServerBillId(baseResponse.getData().getServerBillId());
                    ticketInfo.setJlbh(baseResponse.getData().getJlbh());
                    listener.onPreparePaySuccess(ticketInfo,isPromotion);
                }else {
                    if (TextUtils.isEmpty(baseResponse.getTips())){
                        listener.onPreparePayFailure(baseResponse.getMsg());
                    }else {
                        listener.onPreparePayFailure(baseResponse.getTips());
                    }

                }

            }

            @Override
            public void onFailure(String msg) {
                listener.onPreparePayFailure(msg);

            }
        });

    }

    @Override
    public void QueryOrderInfoEexcute(QueryOrderReq req, final onQueryOrderListener onQueryOrderListener) {
        //发起网络请求 成功回调
        PosHttpManager.QueryOrderInfoEexcute(req,new OkHttpUtils.ResultCallback() {
            @Override
            public void onSuccess(JsonObject response) {

                Log.i("TAG-----订单查询",response.toString());

                try {
                    BaseResponse<RefundOrder> baseResponse = BaseResponse.fromJson(response.toString(), RefundOrder.class);
                    if (baseResponse.getCode().equals("0")){
                        onQueryOrderListener.onQueryOrderSuccess(baseResponse.getData().getOrderList(),baseResponse.getData().getOrderCount());
                    }else {
                        if (TextUtils.isEmpty(baseResponse.getTips())){
                            onFailure(baseResponse.getMsg());
                        }else {
                            onFailure(baseResponse.getTips());
                        }
                    }
                }catch (Exception e){
                    onFailure("数据解析失败"+e.toString());
                }
            }

            @Override
            public void onFailure(String msg) {
                onQueryOrderListener.onQueryOrderFailure(msg);
            }
        });
    }

    public interface OnLoadQueryFoodListener{
        void onLoadQueryFoodSuccess(List<CyFood> FoodList);
        void onLoadQueryFoodFailure(String errorMsg);

    }
}
